package com.kacheap.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.kacheap.service.ScheduledTaskService;

import jakarta.annotation.Resource;

@Controller
public class TaskController {
	@Resource
	private ScheduledTaskService scheduledTaskService;

	@GetMapping("/")
	public ModelAndView index() {
		ModelAndView modelAndView = new ModelAndView("index");
		modelAndView.addObject("tasks", scheduledTaskService.tasks());
		return modelAndView;
	}

	@PostMapping("/add-task")
	public String addTask(@RequestParam String taskName, @RequestParam String expression) {
		scheduledTaskService.add(taskName, expression);
		return "redirect:/";
	}

	@GetMapping(value = "cancel/{taskName}")
	public Object cancel(@PathVariable String taskName) {
		scheduledTaskService.cancel(taskName);
		return "redirect:/";
	}

}